package handler

import (
	"context"
	"encoding/json"
	"github.com/google/uuid"
	"log"
	"strconv"
	"time"
	"user-server/basic/global"
	"user-server/inaternal/model"
	pb "user-server/proto"
)

type UserServer struct {
	pb.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *UserServer) Add(_ context.Context, in *pb.AddReq) (*pb.AddResp, error) {
	var videos model.Video
	global.DB.Where("title = ?", in.Title).Find(&videos)
	if videos.Id != 0 {
		return &pb.AddResp{
			Msg:  "视频已经存在，不允许上传",
			Code: 500,
		}, nil
	}
	vidoe := model.Video{
		Title: in.Title,
		Text:  in.Text,
	}
	global.DB.Create(&vidoe)
	return &pb.AddResp{
		Msg:  "add is success",
		Code: 200,
	}, nil
}
func (s *UserServer) List(_ context.Context, in *pb.ListReq) (*pb.ListResp, error) {
	var videos []model.Video
	page := in.Page
	if page <= 0 {
		page = 1
	}
	pageSize := in.Size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	offset := (page - 1) * pageSize
	key := "video_list_page_" + strconv.Itoa(int(in.Page)) + "_size_" + strconv.Itoa(int(in.Size))
	ctx := context.Background()
	bytes, err := global.RDB.Get(ctx, key).Bytes()
	if err == nil {
		err = json.Unmarshal(bytes, &videos)
		if err != nil {
			log.Println("解析失败，继续从mysql中查询")
		} else {
			if in.Page != 0 && in.Size != 0 {
				global.DB.Offset(int(offset)).Limit(int(pageSize)).Find(&videos)
			} else {
				global.DB.Find(&videos)
			}
			var list []*pb.Item
			for _, i2 := range videos {
				lists := pb.Item{
					Title: i2.Title,
					Text:  i2.Text,
				}
				list = append(list, &lists)
			}
			return &pb.ListResp{
				List: list,
			}, nil
		}
	}
	if in.Page != 0 && in.Size != 0 {
		global.DB.Offset(int(offset)).Limit(int(pageSize)).Find(&videos)
	} else {
		global.DB.Find(&videos)
	}
	marshal, err := json.Marshal(&videos)
	if err == nil {
		global.RDB.Set(ctx, key, marshal, time.Minute*5)
	}
	var list []*pb.Item
	for _, i2 := range videos {
		lists := pb.Item{
			Title: i2.Title,
			Text:  i2.Text,
		}
		list = append(list, &lists)
	}
	return &pb.ListResp{
		List: list,
	}, nil
}
func (s *UserServer) Register(_ context.Context, in *pb.RegisterReq) (*pb.RegisterResp, error) {
	var user model.User
	global.DB.Where("name = ?", in.Name).Find(&user)
	if user.Id != 0 {
		return &pb.RegisterResp{
			Msg:  "用户已经存在，不能注册",
			Code: 500,
		}, nil
	}
	number := uuid.NewString()
	us := model.User{
		Name:    in.Name,
		Text:    in.Text,
		Address: in.Address,
		Number:  number,
		VideoId: in.VideoId,
	}
	global.DB.Create(&us)
	return &pb.RegisterResp{
		Msg:  "register is success",
		Code: 200,
	}, nil
}

// 用户列表
func (s *UserServer) UserList(_ context.Context, in *pb.UserListReq) (*pb.UserListResp, error) {
	var user []model.User
	if in.Id != 0 {
		global.DB.Where("id = ?", in.Id).Find(&user)
	} else {
		global.DB.Find(&user)
	}
	var list []*pb.UserItem
	for _, m := range user {
		lists := pb.UserItem{
			Name:    m.Name,
			Text:    m.Text,
			Address: m.Address,
			VideoId: m.VideoId,
		}
		list = append(list, &lists)
	}
	return &pb.UserListResp{
		List: list,
	}, nil
}
